﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.Windows.Forms;
using DevExpress.XtraSplashScreen;

namespace XCI.Windows.Helper
{
    /// <summary>
    /// 闪屏管理
    /// </summary>
    public static class SplashHelper
    {
        private static Type _splashType;
        private static Type _waitType;
        private static IOverlaySplashScreenHandle _defaultHandle;

        /// <summary>
        /// 是否注册闪屏窗体
        /// </summary>
        public static bool IsRegisterSplash => _splashType != null;

        /// <summary>
        /// 是否注册等待窗体
        /// </summary>
        public static bool IsRegisterWait => _waitType != null;

        /// <summary>
        /// 注册闪屏窗体
        /// </summary>
        /// <param name="splashType">闪屏窗体类型</param>
        public static void RegisterSplash(Type splashType)
        {
            _splashType = splashType;
        }

        /// <summary>
        /// 注册等待窗体
        /// </summary>
        /// <param name="waitType">等待窗体类型</param>
        public static void RegisterWait(Type waitType)
        {
            _waitType = waitType;
        }

        /// <summary>
        /// 显示闪屏
        /// </summary>
        /// <param name="message">消息内容</param>
        public static void ShowSplash(string message)
        {
            ShowSplash(message, null);
        }

        /// <summary>
        /// 显示闪屏
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="parentForm">父窗口</param>
        public static void ShowSplash(string message, Form parentForm)
        {
            if (_splashType == null) return;
            if (SplashScreenManager.Default == null)
            {
                SplashScreenManager.ShowForm(parentForm, _splashType, false, false, false);
            }

            if (SplashScreenManager.Default != null)
            {
                SplashScreenManager.Default.SendCommand(SplashCommand.ShowMessage, message);
            }
        }

        /// <summary>
        /// 关闭闪屏
        /// </summary>
        public static void CloseSplash()
        {
            if (_splashType == null) return;
            SplashScreenManager.CloseForm(false, 0, null);
        }

        /// <summary>
        /// 显示等待消息
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="parentForm">父窗口</param>
        public static void ShowWait(string msg, Form parentForm = null)
        {
            if (_waitType == null) return;
            if (SplashScreenManager.Default == null)
            {
                SplashScreenManager.ShowForm(parentForm, _waitType, true, true);
            }
            if (SplashScreenManager.Default != null)
            {
                SplashScreenManager.Default.SendCommand(WaitFormCommand.ShowMessage, msg);
            }
        }

        /// <summary>
        /// 关闭等待消息
        /// </summary>
        public static void CloseWait()
        {
            if (_waitType == null) return;
            SplashScreenManager.CloseForm(false, 0, null);
        }

        /// <summary>
        /// 显示加载动画窗体
        /// </summary>
        /// <param name="owner">所属窗体</param>
        /// <returns>返回动画句柄</returns>
        public static IOverlaySplashScreenHandle ShowProgressPanel(Control owner)
        {
            return ShowProgressPanel(owner, null);
        }

        /// <summary>
        /// 显示加载动画窗体
        /// </summary>
        /// <param name="owner">所属窗体</param>
        /// <param name="options">选项</param>
        /// <returns>返回动画句柄</returns>
        public static IOverlaySplashScreenHandle ShowProgressPanel(Control owner, OverlayWindowOptions options)
        {
            if (options == null)
            {
                options = OverlayWindowOptions.Default;
            }
            _defaultHandle = SplashScreenManager.ShowOverlayForm(owner, options);
            return _defaultHandle;
        }

        /// <summary>
        /// 关闭加载动画窗体
        /// </summary>
        public static void CloseProgressPanel()
        {
            CloseProgressPanel(_defaultHandle);
        }

        /// <summary>
        /// 关闭加载动画窗体
        /// </summary>
        /// <param name="handle">动画句柄</param>
        public static void CloseProgressPanel(IOverlaySplashScreenHandle handle)
        {
            if (handle != null)
            {
                SplashScreenManager.CloseOverlayForm(handle);
            }
        }
    }

    /// <summary>
    /// 等待窗体命令类型
    /// </summary>
    public enum WaitFormCommand
    {
        /// <summary>
        /// 显示消息
        /// </summary>
        ShowMessage
    }


    /// <summary>
    /// 闪屏命令类型
    /// </summary>
    public enum SplashCommand
    {
        /// <summary>
        /// 显示消息
        /// </summary>
        ShowMessage
    }
}